<template>
  <!-- 搜索结果页-头部导航 -->
  <div class="search-result-container">
    <!-- 点击实现后退效果 -->
    <van-nav-bar
      title="搜索结果"
      left-arrow
      @click-left="$router.go(-1)"
      fixed
    />
    <div class="search-list_content">
      <van-list v-model="loading"
            :finished="done"
            :immediate-check="false"
            offset="50"
            finished-text="没有更多了"
            @load="pullUpLoadMoreFn">
        <ArticleItem v-for="item in resultList" :key="item.art_id" :obj="item" :isShowCross="false"></ArticleItem>
      </van-list>

    </div>
  </div>
</template>

<script>
import { getSearchResultListAPI } from '@/api/search'
let page = 1
export default {
  name: 'SearchResultList',
  data () {
    return {
      resultList: [], // 搜索结果列表
      loading: false,
      done: false,
      isShowCross: false
    }
  },
  activated () {
    this.getSearchResultList()
  },
  // 组件缓存后deactivated
  // 组件切走时执行
  deactivated () {
    page = 1
    this.resultList = []
  },
  computed: {
    txt () {
      return this.$route.params.keyword
    }
  },
  methods: {
    async getSearchResultList () {
      const resp = await getSearchResultListAPI({
        keyword: this.txt,
        page: page++
      })
      const { results } = resp.data
      if (results.length < 10) {
        this.done = true
        return
      }
      // 把新数组 与 原来的数组进行合并
      this.resultList = [...this.resultList, ...results]
      // 表示可以进行下一次请求了
      this.loading = false
    },
    // 上拉加载更多
    pullUpLoadMoreFn () {
      // 切换频道时，只有 articleList 不为空，才触发 上拉加载更多
      if (this.resultList.length > 0) {
        // 发请求，拿数据、合并
        this.getSearchResultList()
      }
    }
  }
}
</script>

<style lang="less" scoped>
.search-result-container {
  padding-top: 46px;
}
</style>
